import gtk

from widgetOrganizer import WidgetOrganizer 
from notebook import Notebook 
from toolbar import MainToolbar 
from menu import MainMenu

class MainWindow(WidgetOrganizer, gtk.Window):
    '''
    The toplevel window of symbide.
    
    It has only one child:
        -MainVBox(gtk.VBox)
    '''
    
    def __init__(self, settings, parsers, executor):
        self.settings = settings
        self.parsers = parsers
        self.executor = executor
        
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        WidgetOrganizer.__init__(self, 'MainWindow')
        
        self.connect('delete_event', self._event_close)
        
        #TODO: capture the size of the last session
        self.resize(600, 500)
        
        #create MainVBox-widget
        child = MainVBox(self)
        self.addChild(child)
        self.add(child)
         
    def _event_close(self, *args):
        '''
        Closes all children in the widget tree, but gives them time to clean up.
        
        First ask for saving any changings, if this is done and the user hasn't
        aborted, stores the open worksheets for the next session. Afterwards it
        removes the childTree and gives widgets a chance to react.
        '''
        
        close = self.children[0].notebook.saveChangedWorksheets()
        if close:
            gtk.main_quit()
            self.children[0].notebook.storeWorksheets()
            self.remove()
        else:
            #if the user aborts the closing, capture the event
            return True


class MainVBox(WidgetOrganizer, gtk.VBox):
    '''
    A gtk.VBox that stores:
        -notebook
        -mainToolbar
        -mainMenu
    '''
    
    def __init__(self, ancestor):
        
        gtk.VBox.__init__(self)
        WidgetOrganizer.__init__(self, 'MainVBox', ancestor)
        
        #create subwidgets
        children = Notebook(self), MainToolbar(self), MainMenu(self)
        expand = True, False, False
        
        for i in range(len(children)):
            self.addChild(children[i])
            self.pack_end(children[i], expand=expand[i])
            
        #sometimes widgets have to acces another widget-tree. then it's
        #useful to call them by a name, not with lists.
        self.notebook = children[0]
        self.mainToolbar = children[1]
        self.mainMenu = children[2]
    
    def removeChild(self, child=None):
        '''
        Removes a child from the widget tree.
        
        It is necessary to override the function of WidgetOrganizer, because
        there are additional references to delete.
        '''
        
        ref = [self.notebook, self.mainToolbar, self.mainMenu]
        
        if child and child in ref:
            del ref[ref.index(child)]
        else:
            for i in range(len(ref)):
                del ref[0]


def run(*args):
    '''
    creates the toplevel window, enables threads and enters the mainloop
    '''
    
    m=MainWindow(*args)
    gtk.gdk.threads_init()
    gtk.main()